<template>
  <v-combobox
    v-bind="$attrs"
    v-model="selection"
    :items="items"
    :item-text="itemText"
    :item-value="itemValue"
    :loading="loading"
    v-on="$listeners"
  ></v-combobox>
</template>

<script>
import request from '@/utils/request'
export default {
  name: 'DictCombobox',
  props: {
    value: {
      type: [String, Array]
    },
    type: {
      type: String,
      required: true
    },
    itemText: {
      type: String,
      default: 'value'
    },
    itemValue: {
      type: String,
      default: 'id'
    },
    api: {
      type: String,
      default: '/admin/system/datadict/listbytype'
    }
  },
  computed: {
    selection: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  created() {
    this.getList()
  },
  data() {
    return {
      loading: false,
      items: []
    }
  },
  methods: {
    getList() {
      this.loading = true
      request({
        url: this.api,
        method: 'get',
        params: { type: this.type }
      }).then(res => {
        this.items = res.data.map(item => item.value)
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style>

</style>